#include "MainScene.h"
#include "AboutScene.h"
#include "SpriteScene.h"
#include "LabelScene.h"
#include "FontScene.h"
#include "ActionScene.h"

using namespace cocos2d;
#define  SCENE_COUNT 5
#define  SCENE_CREAT_FUNC(SN) (unsigned long)SN##Scene::create

typedef CCScene * (*SceneCreateFunc)(void);

const std::string g_aTestNames[SCENE_COUNT]={
	"Sprite",
	"Label",
	"Font",
	"Action",
	"About"
};

const unsigned long g_aTestSceneCreateFuncs[SCENE_COUNT]=
{
	SCENE_CREAT_FUNC(Sprite),
	SCENE_CREAT_FUNC(Label),
	SCENE_CREAT_FUNC(Font),
	SCENE_CREAT_FUNC(Action),
	SCENE_CREAT_FUNC(About)
};


CCScene * MainScene::scene()
{
	return MainScene::create();
}

bool MainScene::init()
{
	bool ret = false;
	do 
	{
		CC_BREAK_IF(!CCScene::init());
		bgLayer = CCLayer::create();
		menuLayer = CCLayer::create();

		this->addChild(bgLayer,1);
		this->addChild(menuLayer,2);

		CCMenu * menu = CCMenu::create();
		for (int i = 0; i < SCENE_COUNT; i++)
		{
			CCMenuItemFont * menuItem = CCMenuItemFont::create(g_aTestNames[i].c_str(),this,menu_selector(MainScene::menuCallback));
			menuItem->setTag(i);
			menu->addChild(menuItem);
		}

		CCSize winSize = CCDirector::sharedDirector()->getWinSize();
		menu->setContentSize(CCSize(winSize.width / 2, winSize.height));
		menu->alignItemsVertically();
		menu->setPosition(ccp( winSize.width - winSize.width / 4 , winSize.height / 2));
		menuLayer->addChild(menu);


		CCSprite *bg = CCSprite::create("burst.png");
		bgLayer->addChild(bg);
		bg->setPosition(ccp(winSize.width / 4, winSize.height / 2));
		ret = true;
	} while (false);
	return ret;
}

void MainScene::menuCallback( cocos2d::CCObject * pSender )
{
	int createFuncIndex = ((CCMenuItem*)pSender) ->getTag();
	SceneCreateFunc createFunc =(SceneCreateFunc) g_aTestSceneCreateFuncs[createFuncIndex];
	CCDirector::sharedDirector()->pushScene(createFunc());
}
